package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.MoreFunctionAdapter;
import com.example.capsule_chat0322.bean.FunctionBean;
import com.example.capsule_chat0322.bean.SignBean;
import com.example.capsule_chat0322.connect.PositionOperation;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.FunctionItemOnClickInterface;
import com.example.capsule_chat0322.request.RoomRequest;
import com.example.capsule_chat0322.request.SignRequest;
import com.example.capsule_chat0322.utils.AlterUtils;


import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/15 20:14
 */
public class MoreFunctionActivity extends AppCompatActivity {

    private List<FunctionBean> functionBeanList;
    private RecyclerView functionRecyclerView;
    private MoreFunctionAdapter functionAdapter;
    private String roomName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_function_list);
        initView();//初始化UI控件
    }

    private void initView() {

        functionRecyclerView = findViewById(R.id.more_function_recycler_view);
        roomName = getIntent().getStringExtra("roomName");
        if (roomName == null) {
            return;
        }
        doGetFunctionList(roomName);


    }

    private void doGetFunctionList(String roomName) {
        RoomRequest.getFunctionList(this, new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {

                List<FunctionBean> functionBeans = (List<FunctionBean>) message.obj;
                if (functionBeans != null) {
                    functionBeanList = functionBeans;
                    LinearLayoutManager layoutManager = new LinearLayoutManager(MoreFunctionActivity.this);
                    functionRecyclerView.setLayoutManager(layoutManager);
                    functionAdapter = new MoreFunctionAdapter(functionBeanList);
                    adapterAddListener(functionAdapter);
                    functionRecyclerView.setAdapter(functionAdapter);
                }

                return true;
            }
        }), roomName);

    }


    public void adapterAddListener(MoreFunctionAdapter functionAdapter) {

        if (functionAdapter == null) {
            return;
        }

        functionAdapter.setFunctionItemOnClickInterface(new FunctionItemOnClickInterface() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position, String functionName, String roomName) {

                Intent intent = new Intent();
                if (functionName.contains("聊天记录") && (functionName.contains("实时检索") || functionName.contains("搜索"))) {
                    intent.putExtra("roomName", roomName);
                    intent.setClass(MoreFunctionActivity.this, SearchRecordActivity.class);
                    startActivity(intent);
                } else if (functionName.contains("附近的人")) {
                    PositionOperation.getLocation(MoreFunctionActivity.this, new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(@NonNull Message message) {
                            intent.setClass(MoreFunctionActivity.this, NearPeopleActivity.class);
                            startActivity(intent);
                            return true;
                        }
                    }));

                } else if (functionName.contains("群聊在线好友")) {
                    intent.setClass(MoreFunctionActivity.this, ChatGroupFriendActivity.class);
                    intent.putExtra("roomName", roomName);
                    startActivity(intent);
                } else if (functionName.contains("签到")) {

                    boolean isOpen = PositionOperation.checkGPSIsOpen(MoreFunctionActivity.this);
                    if (!isOpen) {
                        AlterUtils.alertByToastInMainThread(MoreFunctionActivity.this, "请打开定位");
                        return;
                    }

                    if (GlobalAssistMap.getObject("cannotSign") != null) {
                        AlterUtils.alertByToastInMainThread(MoreFunctionActivity.this, "签到时间已过");
                        return;
                    }

                    SignRequest.judgeHasSign(new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(@NonNull Message message) {
                            SignBean signBean = (SignBean) message.obj;
                            if (signBean.getSignStatus() == 2) {
                                GlobalAssistMap.putObject("cannotSign", "true");
                                AlterUtils.alertByToastInMainThread(MoreFunctionActivity.this, "签到时间已过");
                            } else {
                                intent.setClass(MoreFunctionActivity.this, SignActivity.class);
                                GlobalAssistMap.putObject("signBean", signBean);
                                intent.putExtra("roomName", roomName);
                                startActivity(intent);
                            }
                            return true;
                        }
                    }), roomName, GlobalAssistMap.getObject("userId").toString());

                }

            }
        });
    }

}
